local heming = fk.CreateSkill {
  name = "3s__heming",
  tags = { Skill.Switch },
}

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["3s__heming"] = "合鸣",
  [":3s__heming"] = "转换技，①每回合限一次，你可视为使用无次数限制的【杀】，然后令目标角色将手牌数调整至其体力值；②每回合限一次，你可以将一名角色的一张牌当【闪】使用，然后令其将手牌数摸至其体力上限。",
  [":3s__heming_yang"] = "转换技，<font color=\"#0FFF50\">①每回合限一次，你可视为使用无次数限制的【杀】，然后令目标角色将手牌数调整至其体力值；</font>②每回合限一次，你可以将一名角色的一张牌当【闪】使用，然后令其将手牌数摸至其体力上限。",
  [":3s__heming_yin"] = "转换技，①每回合限一次，你可视为使用无次数限制的【杀】，然后令目标角色将手牌数调整至其体力值；<font color=\"#0FFF50\">②每回合限一次，你可以将一名角色的一张牌当【闪】使用，然后令其将手牌数摸至其体力上限。</font>",

  ["#3s__heming-slash"] = "合鸣：你可以视为使用无次数限制的【杀】",
  ["#3s__heming-jink"] = "合鸣：你可以将一名角色的一张牌当【闪】使用，先确定再选角色",
   ["#3s__heming-choose"] = "合鸣：请选择一名有牌的角色",
  ["#3s__heming-jink-card"] = "合鸣：选择 %dest 的一张牌当【闪】使用",
  
  ["$3s__heming1"] = "合则鸣，分则乱！",
  ["$3s__heming2"] = "兄弟齐心，其利断金！",
}

heming:addEffect("viewas", {
  pattern = "slash,jink",
  prompt = function (self, player, selected_cards, selected_targets)
    if player:getSwitchSkillState(heming.name, false) == fk.SwitchYang then
      return "#3s__heming-slash"
    else
      return "#3s__heming-jink"
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 then return end
    if player:getSwitchSkillState(heming.name, false) == fk.SwitchYang then
      local slash = Fk:cloneCard("lvshi__slash")
      slash.skillName = heming.name
      return slash
    else
      local jink = Fk:cloneCard("jink")
      jink.skillName = heming.name
      return jink
    end
  end,
  enabled_at_play = function(self, player)
    return player:getSwitchSkillState(heming.name, false) == fk.SwitchYang
    and player:getMark("heming-slash-turn") == 0
  end,
  enabled_at_response = function(self, player, response)
    if not response then
      if player:getSwitchSkillState(heming.name, false) == fk.SwitchYang then
        return player:getMark("heming-slash-turn") == 0
      else
        return player:getMark("heming-jink-turn") == 0
      end
    end
  end,
  before_use = function(self, player, use)
    local room = player.room
    if player:getSwitchSkillState(heming.name, true) == fk.SwitchYang then
      player:addCardUseHistory("slash", -1)
      room:addPlayerMark(player, "heming-slash-turn")
    else
      local tos = table.filter(room:getAlivePlayers(), function (p) return not p:isNude() end)
      if #tos == 0 then
        U.SetSwitchSkillState(player, heming.name, fk.SwitchYin)
        return ""
      end
      local to = room:askToChoosePlayers(player, {
        targets = tos,
        max_num = 1,
        min_num = 1,
        skill_name = heming.name,
        cancelable = false,
        prompt = "#3s__heming-choose",
      })
      if #to == 0 then
        U.SetSwitchSkillState(player, heming.name, fk.SwitchYin)
        return ""
      end
      local card = player.room:askToChooseCard(player, {
        skill_name = heming.name,
        target = to[1],
        flag = "he",
        prompt = "#3s__heming-jink-card::"..to[1].id,
      })
      if card then
        room:addPlayerMark(player, "heming-jink-turn")
        use.card:addSubcard(card)
        use.extra_data = use.extra_data or {}
        use.extra_data.target = to[1]
      else
        return ""
      end
    end
  end,
  after_use = function(self, player, use)
    local room = player.room
    if player:getSwitchSkillState(heming.name, true) == fk.SwitchYang then
      for _, p in ipairs(use.tos) do
        -- 令目标角色将手牌数调整至其体力值
        if p:getHandcardNum() ~= p.hp then
          local n = math.abs(p:getHandcardNum() - p.hp)
          if p:getHandcardNum() > p.hp then
            room:askToDiscard(p, {
              skill_name = heming.name,
              cancelable = false,
              min_num = n,
              max_num = n,
              include_equip = false,
            })
          else
            p:drawCards(n, heming.name)
          end
        end
      end
    elseif use.extra_data and use.extra_data.target then
      local target = use.extra_data.target
      -- 令其将手牌数摸至其体力上限
      if target:getHandcardNum() < target.maxHp then
        local n = target.maxHp - target:getHandcardNum()
        target:drawCards(n, heming.name)
      end
    end
  end,
})

heming:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card)
    if player:hasSkill(heming.name) and card and card.skillName == heming.name and scope == Player.HistoryPhase then
      return true
    end
  end,
})

return heming